<?php

/**
 * fondos actions.
 *
 * @package    SistemaWebUnlSenescyt
 * @subpackage fondos
 * @author     JFernando
 * @version    SVN: $Id: actions.class.php 23810 2009-11-12 11:07:44Z Kris.Wallsmith $
 */
class fondosActions extends sfActions {

    public function executeIndex(sfWebRequest $request) {
        $this->idp = $request->getParameter('id_pro');
        $this->getUser()->setAttribute('id_pro', $this->idp);
        $this->fondos = Doctrine::getTable('fondo')->createQuery('f')
                ->where('f.id_pro = ?', $this->idp)
                ->execute();

        $q = Doctrine_Query::create()
                ->from('proyecto p')
                ->where('p.id_pro=?', $this->idp);
        $this->proyecto = $q->fetchOne();
    }

    public function executeShow(sfWebRequest $request) {
        $this->fondo = Doctrine_Core::getTable('fondo')->find(array($request->getParameter('id_fo')));
        $this->forward404Unless($this->fondo);
    }

    public function executeNewindex(sfWebRequest $request) {
        $idpro = $request->getParameter('idpro');
        $this->getUser()->setAttribute('idpro', $idpro);
        $s = 12;
        $this->getUser()->setAttribute('nfondos', $s);
        $this->form = new fondoForm();
    }

    public function executeNew(sfWebRequest $request) {
        $nfondo = $request->getParameter('nu');
        $this->getUser()->setAttribute('nfondos', $nfondo);
        if ($nfondo == 1) {
            $this->form = new FondoFormDefecto();
        } else {
            $this->form = new fondoForm();
        }
    }

    public function executeCreate(sfWebRequest $request) {
        $this->forward404Unless($request->isMethod(sfRequest::POST));
        if ($this->getUser()->getAttribute('nfondos') == 1) {
            $this->form = new FondoFormDefecto();
            $this->processForm($request, $this->form);
        } else if ($this->getUser()->getAttribute('nfondos') == 2) {
            $this->form = new fondoForm();
            $this->processForm($request, $this->form);
        }
        if ($this->getUser()->getAttribute('nfondos') == 12) {
            $this->form = new fondoForm();
            $this->processFormindex($request, $this->form);
        }


        $this->setTemplate('new');
    }

    public function executeEdit(sfWebRequest $request) {

        $idf = $request->getParameter('id_fo');
        $q = Doctrine_Query::create()
                ->from('fondo f')
                ->where('f.id_fo = ?', $idf);

        $fondo = $q->fetchOne();
        //1 es defectyo 0 no es defecto
        if ($fondo->getDefecto() == 0) {
            $this->form = new fondoForm($fondo);
        } else {
            $this->form = new FondoFormDefecto($fondo);
        }
    }

    public function executeUpdate(sfWebRequest $request) {
        $this->forward404Unless($request->isMethod(sfRequest::POST) || $request->isMethod(sfRequest::PUT));
        $this->forward404Unless($fondo = Doctrine_Core::getTable('fondo')->find(array($request->getParameter('id_fo'))), sprintf('Object fondo does not exist (%s).', $request->getParameter('id_fo')));

        //$this->form = new fondoForm($fondo);
        if ($fondo->getDefecto() == 0) {
            $this->form = new fondoForm($fondo);
        } else {
            $this->form = new FondoFormDefecto($fondo);
        }

        $this->processForm2($request, $this->form);

        $this->setTemplate('edit');
    }

    public function executeDelete(sfWebRequest $request) {
        $request->checkCSRFProtection();

        $this->forward404Unless($fondo = Doctrine_Core::getTable('fondo')->find(array($request->getParameter('id_fo'))), sprintf('Object fondo does not exist (%s).', $request->getParameter('id_fo')));
        $aux = $fondo->getIdPro();
        $valores = Doctrine::getTable('valor')->createQuery('v')
                ->where('v.id_fo = ?', $fondo->getIdFo())
                ->execute();
        if (count($valores) >= 0) {
            foreach ($valores as $val):
                $val->delete();
            endforeach;
        }
        $fondo->delete();
        $this->redirect('fondos/index?id_pro=' . $aux);
    }

    protected function processFormindex(sfWebRequest $request, sfForm $form) {

        $form->bind($request->getParameter($form->getName()), $request->getFiles($form->getName()));
        if ($form->isValid()) {
            $q = Doctrine_Query::create()
                    ->from('proyecto p')
                    ->where('p.id_pro=?', $this->getUser()->getAttribute('idpro'));
            $proyecto = $q->fetchOne();
            $form->getObject()->setIdPro($this->getUser()->getAttribute('idpro'));
            $form->getObject()->setProyectos($proyecto);
            $form->getObject()->setDefecto(0);
            $fondo = $form->save();


            $objetivos = Doctrine::getTable('objetivo')->createQuery('o')
                    ->where('o.id_pro = ?', $this->getUser()->getAttribute('idpro'))
                    ->execute();
            if (count($objetivos) >= 0) {
                foreach ($objetivos as $ob):
                    $actividades = Doctrine::getTable('actividad')->createQuery('a')
                            ->where('a.id_ob=?', $ob->getIdOb())
                            ->execute();
                    if (count($actividades) >= 0) {
                        foreach ($actividades as $ac) :
                            $fi = explode("-", $ob->getPeriodos()->getFechai());
                            $ff = explode("-", $ob->getPeriodos()->getFechaf());
                            $ai = $fi[0];
                            $mi = $fi[1];
                            $af = $ff[0];
                            $mf = $ff[1];
                            $descripcionesge = Doctrine::getTable('descripciongeneral')->createQuery('d')
                                    ->where('d.id_ob=?', $this->getUser()->getAttribute('idpro'))
                                    ->execute();
                            if (count($descripcionesge) >= 0) {
                                foreach ($descripcionesge as $descripciongeneral) :
                                    if ($ai == $af) {
                                        while ($mi <= $mf) {
                                            $valor = new Valor();
                                            $valor->setIdDg($descripciongeneral->getIdDg());
                                            $valor->setIdAc($ac->getIdAc());
                                            $valor->setMes($mi);
                                            $valor->setAnio($ai);
                                            $valor->setVal(0.00);
                                            $valor->setIdFo($fondo->getIdFo());
                                            $valor->save();
                                            $mi = $mi + 1;
                                        }
                                    } else {
                                        //caso 2
                                        //cuando el periodo es superior a 2 anios 
                                        $totalanios = $af - $ai;
                                        if ($totalanios == 1) {

                                            while ($mi <= 12) {
                                                $valor = new Valor();
                                                $valor->setIdDg($descripciongeneral->getIdDg());
                                                $valor->setIdAc($ac->getIdAc());
                                                $valor->setIdFo($fondo->getIdFo());
                                                $valor->setMes($mi);
                                                $valor->setAnio($ai);
                                                $valor->setVal(0.00);
                                                $valor->save();
                                                $mi = $mi + 1;
                                            }
                                            $aux = 1;
                                            while ($aux <= $mf) {
                                                $valor = new Valor();
                                                $valor->setIdDg($descripciongeneral->getIdDg());
                                                $valor->setIdAc($ac->getIdAc());
                                                $valor->setIdFo($fondo->getIdFo());
                                                $valor->setMes($aux);
                                                $valor->setAnio($af);
                                                $valor->setVal(0.00);
                                                $valor->save();
                                                $aux++;
                                            }
                                        } else {
                                            //caso 3
                                            //cuando el periodo es superior a 3 anios 

                                            if ($totalanios > 1) {

                                                while ($mi <= 12) {
                                                    $valor = new Valor();
                                                    $valor->setIdDg($descripciongeneral->getIdDg());
                                                    $valor->setIdAc($ac->getIdAc());
                                                    $valor->setIdFo($fondo->getIdFo());
                                                    $valor->setMes($mi);
                                                    $valor->setAnio($ai);
                                                    $valor->setVal(0.00);
                                                    $valor->save();
                                                    $mi = $mi + 1;
                                                }

                                                $auxai = $ai + 1;
                                                $auxaf = $af - 1;
                                                while ($auxai <= $auxaf) {
                                                    for ($i = 1; $i <= 12; $i++) {
                                                        $valor = new Valor();
                                                        $valor->setIdDg($descripciongeneral->getIdDg());
                                                        $valor->setIdAc($ac->getIdAc());
                                                        $valor->setIdFo($fondo->getIdFo());
                                                        $valor->setMes($i);
                                                        $valor->setAnio($auxai);
                                                        $valor->setVal(0.00);
                                                        $valor->save();
                                                    }
                                                    $auxai = $auxai + 1;
                                                }
                                                $aux = 1;
                                                while ($aux <= $mf) {
                                                    $valor = new Valor();
                                                    $valor->setIdDg($descripciongeneral->getIdDg());
                                                    $valor->setIdAc($ac->getIdAc());
                                                    $valor->setIdFo($fondo->getIdFo());
                                                    $valor->setMes($aux);
                                                    $valor->setAnio($af);
                                                    $valor->setVal(0.00);
                                                    $valor->save();
                                                    $aux++;
                                                }
                                            }
                                        }
                                    }
                                endforeach;
                            }
                        endforeach;
                    }
                endforeach;
            }
            $this->getUser()->setFlash('notice', 'Entidad Creada Correctamente!');
            $this->redirect('fondos/index?id_pro=' . $fondo->getIdPro());
        }
    }

    protected function processForm(sfWebRequest $request, sfForm $form) {

        $form->bind($request->getParameter($form->getName()), $request->getFiles($form->getName()));
        if ($form->isValid()) {
            $this->idp = $this->getUser()->getAttribute('id_pro');

            $this->forward404Unless($proyecto = Doctrine_Core::getTable('proyecto')->find(array($this->idp)), sprintf('Object proyecto does not exist (%s).', $this->idp));
            if ($this->getUser()->getAttribute('nfondos') == 1) {
                $form->getObject()->setIdPro($this->idp);
                $form->getObject()->setProyectos($proyecto);
                $form->getObject()->setTipoInst(Instituciones_y_Empresas_Publicas);
                $form->getObject()->setDefecto(1);
                $fondo = $form->save();
            } else {
                $fpd = new Fondo();
                $fpd->setIdPro($this->idp);
                $fpd->setProyectos($proyecto);
                $fond = "Secretaria Nacional de Educación Superior Ciencia y Tecnología";
                $fpd->setNInst($fond);
                $fpd->setTipoInst(Instituciones_y_Empresas_Publicas);
                $fpd->setSiglasInst(Senescyt);
                $fpd->setDefecto(1);
                $fpd->save();


                $form->getObject()->setIdPro($this->idp);
                $form->getObject()->setProyectos($proyecto);
                $form->getObject()->setDefecto(0);
                $fondo = $form->save();
            }
            $this->getUser()->setFlash('notice', 'Proyecto Creado Correctamente!');
            $this->redirect('proyectos/index');
        }
    }

    protected function processForm2(sfWebRequest $request, sfForm $form) {
        $form->bind($request->getParameter($form->getName()), $request->getFiles($form->getName()));
        if ($form->isValid()) {
            $fondo = $form->save();
            $this->getUser()->setFlash('notice', 'Entidad Actualizada Correctamente!');
            $this->redirect('fondos/index?id_pro=' . $this->getUser()->getAttribute('id_pro'));
        }
    }

}
